package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RecordTable;
import com.ruoyi.system.domain.RecordType;
import com.ruoyi.system.domain.vo.BarChartVo;
import com.ruoyi.system.mapper.RecordManageChartMapper;
import com.ruoyi.system.mapper.RecordTypeMapper;
import com.ruoyi.system.service.IRecordManageChartService;
import com.ruoyi.system.service.IRecordTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 可视化  Service业务层处理
 *
 * @author tangrs
 * @date 2023-03-06
 */
@Service
public class RecordManageChartServiceImpl implements IRecordManageChartService {
    @Autowired
    IRecordTableService iRecordTableService;
    @Autowired
    RecordManageChartMapper recordManageChartMapper;

    @Autowired
    RecordTypeMapper recordTypeMapper;

    /***
     * 按部门统计：案卷，卷内
     * @param
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BarChartVo> selectChartByDept(Map<String, Object> map) {
        //1.遍历table表
        //2，分别按案卷，卷内统计数据，并遍历叠加
        List<BarChartVo> chartArchList = new ArrayList<>();
        List<RecordTable> tableList = iRecordTableService.selectRecordTableList(null);
        for(RecordTable t : tableList){
            if("1".equals(t.getTbType())){// 案卷信息
                map.put("tbName", t.getTbName());
                this.getList(chartArchList, recordManageChartMapper.selectChartByDept(map));
            }else if("2".equals(t.getTbType())){// 卷内信息
                map.put("tbName", t.getTbName());
                this.getList(chartArchList, recordManageChartMapper.selectChartByDept(map));
            }else if("3".equals(t.getTbType())){// 独立文件信息
                map.put("tbName", t.getTbName());
                this.getList(chartArchList, recordManageChartMapper.selectChartByDept(map));
            }
        }
        return chartArchList;
    }

    /***
     * 按档案期限统计：案卷，卷内
     * @param
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BarChartVo> selectChartByLimit(Map<String, Object> map) {
        //1.遍历table表
        //2，分别按案卷，卷内统计数据，并遍历叠加
        List<BarChartVo> chartArchList = new ArrayList<>();
        List<RecordTable> tableList = iRecordTableService.selectRecordTableList(null);
        for(RecordTable t : tableList){
            if("1".equals(t.getTbType())){
                map.put("tbName", t.getTbName());
                map.put("storeTime", "acStoreTime");
                this.getList(chartArchList, recordManageChartMapper.selectChartByLimit(map));
            }else if("2".equals(t.getTbType()) || "3".equals(t.getTbType())){
                map.put("tbName", t.getTbName());
                map.put("storeTime", "storeTime");
                this.getList(chartArchList, recordManageChartMapper.selectChartByLimit(map));
            }
        }
        return chartArchList;
    }

    /***
     * 按档案类型统计：案卷，卷内
     * @param
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BarChartVo> selectChartByType(Map<String, Object> map) {
        //1.根据table表，查询对应档案类型名称和对应表数据
        List<BarChartVo> chartArchList = new ArrayList<>();
        List<RecordTable> tableList = iRecordTableService.selectRecordTableList(null);
        for(RecordTable t : tableList){
            map.put("tbName", t.getTbName());
            // 查询出档案分类名称
            RecordType rType = recordTypeMapper.selectRecordTypeByTypeId(t.getTypeId());
            if(StringUtils.isNull(rType)){
                throw new ServiceException("请联系管理员：档案表有值，类型表却为空！");
            }
            String name = rType.getTypeName();
            // 查询出对应表的数据总数
            int value = recordManageChartMapper.selectChartByType(map);
            // 封装图表类
            BarChartVo barChartVo = new BarChartVo();
            barChartVo.setName(name);
            barChartVo.setValue(value);
            // 去重叠加
            List<BarChartVo> newList = new ArrayList<>();
            newList.add(barChartVo);
            this.getList(chartArchList, newList);
        }
        return chartArchList;
    }

    /***
     * 按档案类型统计：归档案卷+卷内
     * @param
     * @return
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<BarChartVo> selectChartByTypeArch(Map<String, Object> map) {
        //1.根据table表，查询对应档案类型名称和对应表数据
        List<BarChartVo> chartArchList = new ArrayList<>();
        List<RecordTable> tableList = iRecordTableService.selectRecordTableList(null);
        for(RecordTable t : tableList){
            map.put("tbName", t.getTbName());
            // 查询出档案分类名称
            String name = recordTypeMapper.selectRecordTypeByTypeId(t.getTypeId()).getTypeName();
            // 查询出对应表的数据总数
            int value = recordManageChartMapper.selectChartByTypeArch(map);
            // 封装图表类
            BarChartVo barChartVo = new BarChartVo();
            barChartVo.setName(name);
            barChartVo.setValue(value);
            // 去重叠加
            List<BarChartVo> newList = new ArrayList<>();
            newList.add(barChartVo);
            this.getList(chartArchList, newList);
        }
        return chartArchList;
    }

    /***
     * 按档案类型统计：年度利用
     * @param
     * @return
     */
    @Override
    public List<BarChartVo> selectChartByTypeSearch(Map<String, Object> map) {
        return null;
    }

    /***
     * 按年度统计：年度利用
     * @param
     * @return
     */
    @Override
    public List<BarChartVo> selectChartByYearSearch(Map<String, Object> map) {
        return null;
    }

    // 过滤叠加数据
    private void getList(List<BarChartVo> chartArchList, List<BarChartVo> newList){
        // 如果name相等，vlaue求和，循环所有元素没有匹配的则新增一条新的
        for(BarChartVo c: newList){
            boolean i = true;
            for(BarChartVo chart: chartArchList){
                if(!StringUtils.isEmpty(chart.getName()) && chart.getName().equals(c.getName())){
                    chart.setValue(c.getValue() + chart.getValue());
                    i = false;
                    break;
                }
            }
            if(i){
                chartArchList.add(c);
            }
        }
    }

}
